//
//  NetworkTools.swift
//  afn封装Swi
//
//  Created by user on 16/4/18.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit
import AFNetworking


enum FANRequestMethod:String {
 
    case get = "get"
    
    case post = "post"
    
}


class NetworkTools: AFHTTPSessionManager {
    
    static let sharedTools:NetworkTools = {
    
        let tools = NetworkTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        // text/plain
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")

        return tools
    }()
    
    typealias callBackColsure = (responseObject: AnyObject?, error:NSError?)->()
    
    
    func update(urlString:String, parameter:AnyObject?, datas:[String:NSData],finished:callBackColsure){
        
        
        self.POST(urlString, parameters: parameter, constructingBodyWithBlock: { (formData) -> Void in
            
            for (key,value) in datas{
                
                formData.appendPartWithFileData(value, name: key, fileName: "hah.png", mimeType: "application/octet-stream")
            }
            
            
            }, progress: nil, success: { (_, responseObject) -> Void in
                
                finished(responseObject: responseObject, error: nil)

                
            }) { (_, error) -> Void in
                
                finished(responseObject: nil, error: error)

                
        }
        
    }
    
    func request(method:FANRequestMethod = .post, urlString:String, parameter:AnyObject?, progress:AnyObject?,finished:callBackColsure){
        
        let success = { (dataTask:NSURLSessionDataTask, responseObject:AnyObject?) -> ()in
            
            finished(responseObject: responseObject, error: nil)
            
        }
        
        let failure = { (dataTask:NSURLSessionDataTask?, error:NSError) -> () in
            
            finished(responseObject: nil, error: error)
        }
        
        if method == FANRequestMethod.get{
            
            GET(urlString, parameters: parameter, progress: nil, success: success, failure:failure)
            
        }else{
            
            POST(urlString, parameters: parameter, progress: nil, success: success, failure: failure)
        }
        
    }
    
    
//    func demo(){
//        
//        self.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, progress: <#T##((NSProgress) -> Void)?##((NSProgress) -> Void)?##(NSProgress) -> Void#>, success: <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)
//    }

}
